require(['../config/requireconfig'],function(config){
	//根据需要添加
	require(['jquery','common/pccommons'],function($,com){
		
        com.init({headerPos:{menu:2,sub:1}});
		var pageLogic = new PageLogic($,com);
		pageLogic.init();

	});
});
var PageLogic = function($,com){
	return{
		init:function(){
			var _this = this;
			_this.initData();
		},
		initData:function(){
			var _this =this;
			var partsInfoId = com.utils().urlParam("partsInfoId");
			var data = {};
			data.parsInfoid = partsInfoId;
			com.ajax({
				url:'/partsCategoryDetailInfo.json',
				data:data,
				success:function(res){
					_this.initCategoryTab(res.data.categoryInfo);
					_this.setBasicInfo(res.data.partInfo);
					_this.setPics(res.data.partImgs);
					_this.setItems(res.data.partItems);
					_this.setVedio(res.data.partVedios);
					if(res.data.categoryInfo){
						_this.categoryInfo = res.data.categoryInfo;
						_this.bindEvents();	
					}
				}
			})
		},
		bindEvents:function(){
			var _this = this;
			$(".category-tab").find("li").on("click",function(){
				$(".category-tab").find("li").removeClass("active");
				$(this).addClass("active");
				var tg = $(this).attr("tg");
				$(_this.categoryInfo).each(function(){
					if(tg == this.id){
						var subHtml = "";
						$(this.parts).each(function(){
							var subCls = "";
							if(this.active){
								subCls = "active";
							}
							if(subHtml){
								subHtml += '<span>/<span><a tg="' + this.id + '" href="/prodCategoryDetail.htm?partsInfoId=' + this.id + '"  '  + (subCls?'class="active"':'') + '>' + this.partsName + '</a>';
							}else{
								subHtml += '<a href="/prodCategoryDetail.htm?partsInfoId=' + this.id + '" '  + (subCls?'class="active"':'') + '>' + this.partsName + '</a>';
							}
							$("#detailSubMenu").html(subHtml);
						});
					}
				})
				$("#detailSubMenu").html();	
			});
		},
		initCategoryTab:function(categoryInfo){
			var _this = this;
			if(categoryInfo){
				var html ="";
				var subHtml ="";
				$(categoryInfo).each(function(){
					var cls = "";
					if(this.active){
						cls = "active";
					}
					html += ' <li tg="' + this.id + '" ' + (cls?'class="active"':'') + '>' + this.categoryName + '<i class="blue-arrow"></i><div class="dot-line"></div></li>';
					if(this.active){
						$(this.parts).each(function(){
							var subCls = "";
							if(this.active){
								subCls = "active";
							}
							if(subHtml){
								subHtml += '<span>/<span><a tg="' + this.id + '" href="/prodCategoryDetail.htm?partsInfoId=' + this.id + '" '  + (subCls?'class="active"':'') + '>' + this.partsName + '</a>';
							}else{
								subHtml += '<a href="/prodCategoryDetail.htm?partsInfoId=' + this.id + '"  '  + (subCls?'class="active"':'') + '>' + this.partsName + '</a>';
							}
						});
					}
				});
				$(".category-tab").html(html);
				_this.setCategoryTab();
				$("#detailSubMenu").html(subHtml);
			}
		},
		setCategoryTab:function(){
			var width = $(".container").width();
			var allLiWidth = 0;
			$(".category-tab").find("li").each(function(){
				allLiWidth += $(this).width();
			});
			var space = (width - allLiWidth - 6)/10 - 1;
			$(".category-tab").find("li").each(function(idx){
				if(idx < 10){
					$(this).css({"margin-right":space});
				}else{
					$(this).css({"margin-right":0});
				}
			});
		},
		setBasicInfo:function(partInfo){
			if(partInfo){
				$("#prdNm").text(partInfo.partsName || "");
				if(!partInfo.remarksBlank){
					$("#prdRemark").show();
					$("#prdRemark").find(".rmk-detail").text(partInfo.remarks);	
				}
				if(!partInfo.superiorBlank){
					$("#prdsuperior").show();
					$("#prdsuperior").find(".rmk-detail").text(partInfo.superior);	
				}
				if(!partInfo.maintainBlank){
					$("#prdmaintain").show();
					$("#prdmaintain").find(".rmk-detail").text(partInfo.maintain);	
				}
			}
		},
		setPics:function(picInfos){
			var html = "";
			if(picInfos){
				var cls = "";
				$(picInfos).each(function(idx,obj){
					cls = "";
					if(idx == 0){
						$("#leftImg").prop("src",com._Options.domain + obj.imageUrl);
						cls = "active";
					}
					html += '<div class="item ' + cls + '"><img src="' + (com._Options.domain + obj.imageUrl) + '"  /></div>'
				});
			}
			$("#smImgs").html(html);
			$(".pics").find(".item").on("click",function(){
				$(".pics").find(".item").removeClass("active");
				$(this).addClass("active");
				var imgUrl = $(this).find("img").prop("src");
				$("#leftImg").prop("src",imgUrl);
			});
		},
		setVedio:function(obj){
			if(obj && obj.length>0){
				$("#prdVedio").show();
			}
		},
		setItems:function(items){
			if(!items){
				return;
			}
			var html = "";
			$(items).each(function(idx,obj){
				html +='<div class="part-item">';
				html +='	<div class="part-item-img">';
				html +='		<img src="' + (com._Options.domain + obj.imageUrl)  + '" />';
				html +='		<div class="part-item-title">' + obj.itemName + '</div>';
				html +='	</div>';
				html +='	<div class="part-item-content">';
				html +='	    <p class="part-item-content-title">产品介绍</p>';
				html +='		<div class="part-item-content-main">' + com.utils().htmldecode(obj.remarks);
				html +='		</div>';
				html +='	</div>';
				html +='</div>';
			});
			$("#prdItems").html(html);
		}
	}
}